package com.example.myapplication;

import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.common.RequestEnum;
import com.example.util.RegisterEventUtil;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {


    private String[] permissions = new String[]{"android.permission.CAMERA",
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};




    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = new Button(this);
        final Context _this = this;
        requestPermissions(permissions, 200);
        RegisterEventUtil.registerMainButtonAndHandle(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 显示activity 3种方式
                //1
                Intent intent = new Intent(_this, SecondActivity.class);
//                _this.startActivity(intent);
                //2
                Intent intent2 = new Intent();
                intent2.setComponent(new ComponentName(_this, SecondActivity.class.getName()));
//                _this.startActivity(intent2);
                //3
                Intent intent3 = new Intent();
                intent3.setClassName(_this, SecondActivity.class.getName());
//                _this.startActivity(intent3);
                log("点击按钮");
                //隐式调用
                Intent intent14 = new Intent();
                // customize.filter 在主配置种的 intent过滤器中配置的
                Intent intent5 = new Intent();
                intent5.setAction("customize.filter");
                _this.startActivity(intent5);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void log(String msg) {
        Log.d(this.getClass().getSimpleName(), msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("进入回调方法");

        ContentResolver contentResolver = getContentResolver();

        if (RESULT_OK == resultCode) {
            if (RequestEnum.VEDIO.getCode().equals(requestCode)) {
                System.out.println(data);


                return;
            }
            try (InputStream inputStream = contentResolver.openInputStream(data.getData())) {
                FileOutputStream fileOutputStream = openFileOutput("123.txt", MODE_PRIVATE);
                byte[] temp = new byte[1024];
                System.out.println(inputStream.toString());
                int i = -1;
                while ((i = inputStream.read(temp)) != -1) {
                    fileOutputStream.write(temp, 0, i);
                }

            } catch (IOException e) {

                e.printStackTrace();
            }
            Uri data1 = data.getData();
            System.out.println(data1.getPath());


        }
    }
}